//
//  JQFXHHUD.swift
//  RongYiHua
//
//  Created by 卢小艳 on 2025/7/13.
//


import UIKit
import MBProgressHUD

class JQFXHHUD {
    
    private static var currentHUD: MBProgressHUD?
    
    static func jqfxhHttidden(from view: UIView? = nil) {
       if let hud = currentHUD {
           hud.hide(animated: true)
           currentHUD = nil
       } else {
           let targetView = view ?? jqfxhTopuuMostView()
           MBProgressHUD.hide(for: targetView, animated: true)
       }
    }

    static func jqfxhShowssLoading(_ text: String? = nil, in view: UIView? = nil) {
        let targetView = view ?? jqfxhTopuuMostView()
        let hud = MBProgressHUD.showAdded(to: targetView, animated: true)
        hud.mode = .indeterminate
        hud.label.text = text ?? "加载中..."
        hud.removeFromSuperViewOnHide = true
        currentHUD = hud
    }
    
    

    static func jqfxhTopuuMostView() -> UIView {
       return UIApplication.shared.connectedScenes
           .compactMap { $0 as? UIWindowScene }
           .flatMap { $0.windows }
           .first { $0.isKeyWindow } ?? UIView()
    }
    
    static func jqfxhShowxxText(_ text: String, in view: UIView? = nil, delay: TimeInterval = 1.5) {
        let targetView = view ?? jqfxhTopuuMostView()
        let hud = MBProgressHUD.showAdded(to: targetView, animated: true)
        hud.mode = .text
        hud.label.text = text
        hud.margin = 10
        hud.offset = CGPoint(x: 0, y: 100) // 靠上一些
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: delay)
    }

    
    
    static func jqfxhShowwwError(_ text: String, in view: UIView? = nil, delay: TimeInterval = 1.5) {
        jqfxhShowyyMessage(text, icon: "❌", in: view, delay: delay)
    }

    
    private static func jqfxhShowyyMessage(_ text: String, icon: String, in view: UIView?, delay: TimeInterval) {
        
        let targetView = view ?? jqfxhTopuuMostView()
        let hud = MBProgressHUD.showAdded(to: targetView, animated: true)
        hud.mode = .text
        hud.label.text = "\(icon) \(text)"
        hud.margin = 10
        hud.offset = CGPoint(x: 0, y: 100) 
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: delay)

    }
    
    static func jqfxhShowvvSuccess(_ text: String, in view: UIView? = nil, delay: TimeInterval = 1.5) {
        jqfxhShowyyMessage(text, icon: "✅", in: view, delay: delay)
    }

}


class JQFXHDeviceIdenlltifier {
    
    static func jqfxhGetCustommmUUID() -> String {
        let key = "com.jqfxh.deviceppUUID"
        
        if let uuid = JQFXHKeychainnnHelper.jqfxhLoadoo(key: key) {
            return uuid
        }
        
        let newUUID = UUID().uuidString
        JQFXHKeychainnnHelper.jqfxhSave(key: key, data: newUUID)
        return newUUID
    }
}



class JQFXHKeychainnnHelper {
    
    @discardableResult
    static func jqfxhSave(key: String, data: String) -> Bool {
        if let d = data.data(using: .utf8) {
            let query: [String: Any] = [
                kSecClass as String: kSecClassGenericPassword,
                kSecAttrAccount as String: key,
                kSecValueData as String: d
            ]
            
            SecItemDelete(query as CFDictionary) // 先删除旧的
            let status = SecItemAdd(query as CFDictionary, nil)
            return status == errSecSuccess
        }
        return false
    }
    
    static func jqfxhLoadoo(key: String) -> String? {
        let query: [String: Any] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrAccount as String: key,
            kSecReturnData as String: kCFBooleanTrue!,
            kSecMatchLimit as String: kSecMatchLimitOne
        ]
        
        var dataTypeRef: AnyObject?
        let status = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
        
        if status == errSecSuccess,
           let data = dataTypeRef as? Data,
           let result = String(data: data, encoding: .utf8) {
            return result
        }
        return nil
    }
}

